<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  /*body{*/

  /*}*/
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
  input.time {
    border: none;
    font-size: 16px;
    padding-left: 10px;
  }
</style>
<template>
  <div class="oitem">
    <div class="weui-cells__title">货品明细{{itemNo + 1}}
      <p id="acts" style="float: right;color: #1aad19;">
<!--      <p id="acts" style="float: right;color: #1aad19;" @click="addItem" v-if="isLast">-->
<!--        <span id="addItem" style="padding-right: 5px;">添加</span>-->
<!--        <span v-if="itemNo != 0">|</span>-->
        <span id="delItem" style="padding-left: 5px;" @click="delItem(itemdata.guid)" v-if="initItemlen > 1">删除</span>
      </p>
    </div>
    <div class="weui-cell fir">
      <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input" type="hidden" :name="itemsStr + '[' + itemNo + '].flag'" :value="itemdata.flag">
        <input type="hidden" v-if="benifitordeduct == 1 && (benefittype == 1 || benefittype == 2) || benifitordeduct == 2" :name="itemsStr + '[' + itemNo + '].deductrate'" :value="discount">
        <input type="hidden" v-if="itemdata.productid != undefined" :name="itemsStr + '[' + itemNo + '].productid'" :value="itemdata.productid">
        <input type="hidden" v-if="itemdata.productguid != undefined" :name="itemsStr + '[' + itemNo + '].productguid'" :value="itemdata.productguid">
        <input type="text" style="border: 0;font-size: 16px;" readonly :name="itemsStr + '[' + itemNo + '].productname'" :value="itemdata.productname">
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd"><label class="weui-label">规格</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input" type="text" readonly :name="itemsStr + '[' + itemNo + '].spname'" :value="itemdata.spname">
        <input class="weui-input" v-if="itemdata.speguid != undefined " type="hidden" :name="itemsStr + '[' + itemNo + '].speguid'" :value="itemdata.speguid">
        <input class="weui-input" v-if="itemdata.spguid != undefined " type="hidden" :name="itemsStr + '[' + itemNo + '].spguid'" :value="itemdata.spguid">
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd"><label class="weui-label">单价<span>({{payUnit}})</span></label></div>
      <div class="weui-cell__bd">
        <input class="weui-input price inlinput1" type="number" readonly :name="itemsStr + '[' + itemNo + '].price'" v-model="price" placeholder="请输入单价">
        <input class="weui-input inlinput2" type="text" readonly :value="packUnit(itemdata.unit, currency)">
      </div>
    </div>
    <div class="weui-cell" v-if="settletype == 1">
      <div class="weui-cell__hd"><label class="weui-label">订单数量</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input orderpack" style="width: 80%;display: inline-block;" type="number" :name="itemsStr + '[' + itemNo + '].orderpack'" v-model="orderpack" placeholder="需进行结算的数量">
        <input class="weui-input" style="width: 15%;display: inline-block;" type="text" readonly :name="itemsStr + '[' + itemNo + '].unit'" :value="itemdata.unit">
        <p style="text-align: left;font-size: 12px;color: #bbb;padding-top: 5px;">主订单数量:{{itemdata.orderpack}}，未处理数量: {{itemdata.orderpack - itemdata.finishCount}}</p>

      </div>
<!--      <div class="weui-cell__bd" style="display: block">-->
<!--      </div>-->
    </div>
    <div class="weui-cell" v-if="benifitordeduct == 1 && settletype == 1 && benefittype == 5 && paytype < 4">
      <div class="weui-cell__hd"><label class="weui-label">赠送数量</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input" style="width: 80%;display: inline-block;" type="number" :name="itemsStr + '[' + itemNo + '].handselcount'" v-model="handselcount" placeholder="赠送，不结算">
        <input class="weui-input" style="width: 15%;display: inline-block;" type="text" :value="itemdata.unit">
      </div>
    </div>
    <input v-if="benifitordeduct == 1 && settletype == 1 && benefittype == 5 && paytype >= 4" type="hidden" :name="itemsStr + '[' + itemNo + '].handselcount'" v-model="handselcount">
    <div class="weui-cell" v-if="settletype == 2">
      <div class="weui-cell__hd"><label class="weui-label">订单重量</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input weight" style="width: 80%;display: inline-block;" v-model="weight" type="number" :name="itemsStr + '[' + itemNo + '].weight'" placeholder="需进行结算的重量">
        <input class="weui-input" style="width: 15%;display: inline-block;" type="text" readonly :name="itemsStr + '[' + itemNo + '].unit'" :value="itemdata.unit || 吨">
        <p style="text-align: left;font-size: 12px;color: #bbb;padding-top: 5px;">主订单:{{itemdata.weight}}吨，未处理: {{itemdata.weight - itemdata.finishCount}}吨</p>

      </div>
    </div>
<!--    <div class="weui-cell" v-if="benifitordeduct == 1 && settletype == 2 && benefittype == 5 && paytype < 4">-->
<!--      <div class="weui-cell__hd"><label class="weui-label">赠送重量</label></div>-->
<!--      <div class="weui-cell__bd">-->
<!--        <input class="weui-input" style="width: 80%;display: inline-block;" type="number" :name="itemsStr + '[' + itemNo + '].handselweight'" v-model="handselweight" placeholder="赠送，不结算">-->
<!--        <input class="weui-input" style="width: 15%;display: inline-block;" type="text" readonly :value="itemdata.unit || 吨">-->
<!--      </div>-->
<!--    </div>-->
<!--    <input v-if="benifitordeduct == 1 && settletype == 2 && benefittype == 5 && paytype >= 4" type="hidden" :name="itemsStr + '[' + itemNo + '].handselweight'" v-model="handselweight">-->
    <div class="weui-cell">
      <div class="weui-cell__bd">
        <textarea class="weui-textarea" :name="itemsStr + '[' + itemNo + '].remark'" placeholder="请输备注内容" rows="2">{{itemdata.remark}}</textarea>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'DivideOrderItem',
    data () {
      return {
        productid: '',
        specid: '',
        weight: '',
        commonproducts: [],
        specs: [],
        bigorderVal: 40,
        contractProducts: [],
        contractSpecs: [],
        prices: '',
        price: '',
        orderpack: '',
        handselcount: '',
        handselweight: '',
        isEdit: true,
        currProduct: '',
        flag: '',
        currPro: '',
        currAmount: 0
      }
    },
    props: [
      'itemNo',
      'isLast',
      'departid',
      'products',
      'contract',
      'quitsrtype',
      'benifitordeduct',
      'benefittype',
      'discount',
      'settletype',
      'prodpricestra',
      'currency',
      'itemdata',
      'initItemlen',
      'orgid',
      'paytype',
      'childAmount',
      'orderAmount'
    ],
    computed: {
      payUnit: function () {
        return this.settletype == 1 ? '元/件' : '元/吨';
      },
      itemsStr: function () {
        return this.itemdata.sellitems != undefined ? 'spitems' : 'pitems';
      }
      // idata: function () {
      //   return typeof this.itemdata == "object" ? '元/件' : '元/吨';
      // }
    },
    mounted() {
      console.log('finishCount:', this.itemdata.finishCount)
      this.setData();
      // this.getPrice();
    },
    watch: {
      'itemdata': function (val) {
        this.setData();
      },
      orderpack: function (val, oldval) {
        let place = String(val).indexOf(".") + 1;
        if (place > 0) {
          this.orderpack = '';
          this.toastText('订单数量不能为小数');
        }
        // this.currAmount = this.toFix2((val - oldval) * this.price);
        this.currAmount = (val - oldval) * this.price;
        this.computeTotalAmount(this.currAmount);
        if (this.orderAmount <= 0 || this.$parent.totalAmount <= 0) return;
        let totalAmount = this.toFloorFix3(this.$parent.totalAmount);
        // if (totalAmount + this.childAmount > this.orderAmount) {
        //   this.toastText3(`分拆订单金额(${totalAmount}) + 已分拆订单金额(${this.childAmount}) 不能大于主订单金额(${this.orderAmount})`)
        //   this.orderpack = '';
        // }
      },
      weight: function (val, oldval) {
        // this.currAmount = this.toFix2((val - oldval) * this.price);
        this.currAmount = (val - oldval) * this.price;
        this.computeTotalAmount(this.currAmount);
        if (this.orderAmount <= 0 || this.$parent.totalAmount <= 0) return;
        let totalAmount = this.toFloorFix3(this.$parent.totalAmount);
        // if (totalAmount + this.childAmount > this.orderAmount) {
        //   this.toastText3(`分拆订单金额(${totalAmount}) + 已分拆订单金额(${this.childAmount}) 不能大于主订单金额(${this.orderAmount})`)
        //   this.weight = '';
        // }
      },
      'handselcount': function (val) {
        let place = String(val).indexOf(".") + 1;
        if (place > 0) {
          this.handselcount = '';
          this.toastText('赠送数量不能为小数');
        }
        // if (parseInt(val) >= parseInt(this.orderpack)) {
        //   this.toastText('赠送件数必须小于订单件数');
        //   this.handselcount = '';
        // }
      }
    },
    methods: {
      packUnit: function(unit, currency) {
        return unit != undefined && unit != '' ? currency == 2 ? '美元/' + unit : '元/' + unit : '';
      },
      setData: function() {
        this.specid = this.itemdata.speguid != undefined ? this.itemdata.speguid : this.itemdata.spguid;
        //订单数量、重量不回显
        // this.orderpack = this.itemdata.orderpack;
        // this.weight = this.itemdata.weight;
        this.handselcount = this.itemdata.handselcount;
        this.handselweight = this.itemdata.handselweight;
        this.price = this.itemdata.price;
      },
      getPrice: function() {
        if (this.specid == '' || this.contract == undefined || this.currency == undefined || this.settletype == undefined) return;
        this.axios({
          method: 'get',
          url: '/sales/Products!getPrice.action',
          params: {speId: this.specid, contractId: this.contract.guid, currency: this.currency, valuation: this.settletype}
        }).then((response) => {
          if (response.data.msg == 'success') {
            this.prices = response.data.products != undefined ? response.data.products : '';
            console.log('isEdit:', this.isEdit)
            if (this.isEdit) {
              this.price = this.itemdata.price;
              this.isEdit = false;
            } else {
              this.price = this.prices != '' ? this.prices.price : '';
            }
          } else {
            this.toastText('获取价格数据失败')
          }

        })
      },
      addItem: function() {
        this.$emit('doAddItem');
      },
      delItem: function(id) {
        this.$emit('doDelItem', id);
        this.computeTotalAmount(-this.currAmount);
      },
      computeTotalAmount: function (val) {
        this.$emit('computeAmount', val);
      }
    }
  }
</script>
